package com.magicsoft.share.ui.share;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.magicsoft.share.R;
import com.magicsoft.share.adapter.share.CommentAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.share.CommentList;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.customview.alertview.OnItemClickListener;
import com.magicsoft.share.presenter.share.CommentListPresenter;
import com.magicsoft.share.ui.main.LoginActivity;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.share.ICommentListView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import butterknife.BindView;
import butterknife.OnClick;

import static com.magicsoft.share.constant.Constant.REFRESH_UI;

/**
 * @author lyf
 *         评论列表界面
 */
public class CommentListActivity extends BaseActivity<ICommentListView, CommentListPresenter> implements ICommentListView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.tv_send)
    TextView tvSend;
    @BindView(R.id.layout_bottom)
    LinearLayout layoutBottom;
    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView recyclerView;

    private int pageNum = 1;
    private int pageSize = 10;
    private boolean isRefresh = true;
    private CommentAdapter commentAdapter;
    private long articleId;
    private AlertView alertView;

    @Override
    public CommentListPresenter initPresenter() {
        return new CommentListPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_comment_list;
    }

    @Override
    public void initData() {

        articleId = getIntent().getLongExtra("articleId", -1);

    }

    @Override
    public void initView() {

        tvTitle.setText("0条评论");
        Drawable img = this.getResources().getDrawable(R.drawable.comment);
// 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
        tvTitle.setCompoundDrawables(img, null, null, null);
        tvRight.setText("发表评论");
        recyclerView.useDefaultLoadMore();
        recyclerView.loadMoreFinish(true, false);
        recyclerView.setLoadMoreListener(new SwipeMenuRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {

                isRefresh = false;
                pageNum++;
                requestData();
            }
        });


        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg), 0, DensityUtils.dp2px(this, 0.8f)));


        commentAdapter = new CommentAdapter(R.layout.item_comment);
        recyclerView.setAdapter(commentAdapter);

        initDialog();
    }

    @Override
    public void requestData() {

        mPresenter.commentList(articleId,pageNum,pageSize);
    }


    @OnClick({R.id.ibtn_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.tv_right:
                String string = SPUtils.getInstance().getString(SPKey.TOKEN);
                if (!string.equals("") && string != null) {
                    Intent intent = new Intent(this, CommentWriteActivity.class);
                    intent.putExtra("articleId",articleId);
                    startActivityForResult(intent, REFRESH_UI);
                }else {
                    alertView.show();
                }

                break;
            default:
                break;
        }
    }




    @Override
    public void commentListSuccess(CommentList data) {

        if (isRefresh) {
            tvTitle.setText(data.getTotal()+"条评论");
            commentAdapter.setNewData(data.getList());
        } else {
            commentAdapter.addData(data.getList());
        }

        if (pageNum < data.getPages()) {
            recyclerView.loadMoreFinish(false, true);
        } else if (pageNum == data.getPages()) {
            recyclerView.loadMoreFinish(false, false);
        } else {
            recyclerView.loadMoreFinish(true, false);
        }
    }

    @Override
    public void commentListFail() {
        if (isRefresh) {
            recyclerView.loadMoreFinish(true, false);
        } else {
            recyclerView.loadMoreFinish(false, false);
        }
    }


    private void initDialog() {
        alertView = new AlertView.Builder().setContext(this).setTitle("检测到您尚未登录，是否现在登录？").setStyle(AlertView.Style.Alert).setCancelText("否").setOthers(new String[]{"是"}).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {

                if (position == -1) {
                    alertView.dismiss();

                } else {
                    alertView.dismiss();
                    Intent intent = new Intent(CommentListActivity.this, LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);

                }
            }
        }).build();
    }
}
